
#import "SMUserFollowCell.h"
#import <Masonry/Masonry.h>
#import <SDWebImage/UIImageView+WebCache.h>
@implementation SMUserFollowCell {
    UIView *_card; UIImageView *_avatar; UILabel *_nameL; UILabel *_introL; UIButton *_btn;
    BOOL _following;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)rid {
    if (self=[super initWithStyle:style reuseIdentifier:rid]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = UIColor.systemGroupedBackgroundColor;
        _card=[UIView new]; _card.backgroundColor=UIColor.secondarySystemBackgroundColor; _card.layer.cornerRadius=12; _card.layer.masksToBounds=YES;
        [self.contentView addSubview:_card];
        [_card mas_makeConstraints:^(MASConstraintMaker *make){ make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(8,12,8,12)); }];
        _avatar=[UIImageView new]; _avatar.layer.cornerRadius=20; _avatar.clipsToBounds=YES; _avatar.contentMode=UIViewContentModeScaleAspectFill;
        _nameL=[UILabel new]; _nameL.font=[UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        _introL=[UILabel new]; _introL.font=[UIFont systemFontOfSize:12]; _introL.textColor=UIColor.secondaryLabelColor; _introL.numberOfLines=2;
        _btn=[UIButton buttonWithType:UIButtonTypeSystem];
        _btn.titleLabel.font=[UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
        _btn.layer.cornerRadius=14; _btn.contentEdgeInsets=UIEdgeInsetsMake(6,12,6,12);
        [_btn addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
        [_card addSubview:_avatar]; [_card addSubview:_nameL]; [_card addSubview:_introL]; [_card addSubview:_btn];
        [_avatar mas_makeConstraints:^(MASConstraintMaker *make){ make.left.top.equalTo(self->_card).offset(12); make.size.mas_equalTo(CGSizeMake(40,40)); }];
        [_nameL mas_makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(self->_avatar); make.left.equalTo(self->_avatar.mas_right).offset(10); make.right.lessThanOrEqualTo(self->_btn.mas_left).offset(-8);}];
        [_introL mas_makeConstraints:^(MASConstraintMaker *make){ make.top.equalTo(self->_nameL.mas_bottom).offset(4); make.left.equalTo(self->_nameL); make.right.equalTo(self->_card).offset(-12); make.bottom.lessThanOrEqualTo(self->_card).offset(-12);}];
        [_btn mas_makeConstraints:^(MASConstraintMaker *make){ make.centerY.equalTo(self->_avatar); make.right.equalTo(self->_card).offset(-12);}];
    }
    return self;
}
- (void)prepareForReuse { [super prepareForReuse]; [_avatar sd_cancelCurrentImageLoad]; _avatar.image=nil; }
- (void)applyBtn {
    NSString *title = _following ? @"已关注" : @"关注";
    UIColor *bg = _following ? [UIColor colorWithWhite:0.9 alpha:1] : [UIColor colorWithRed:0.23 green:0.56 blue:0.96 alpha:1];
    UIColor *tc = _following ? UIColor.labelColor : UIColor.whiteColor;
    [_btn setTitle:title forState:UIControlStateNormal];
    _btn.backgroundColor = bg;
    [_btn setTitleColor:tc forState:UIControlStateNormal];
}
- (void)tap { _following=!_following; [self applyBtn]; if(self.onToggleFollow) self.onToggleFollow(_following); }
- (void)configureWithAvatar:(NSString *)avatar name:(NSString *)name intro:(NSString *)intro following:(BOOL)following {
    _following=following; [self applyBtn];
    UIImage *ph=nil; if (@available(iOS 13.0,*)) ph=[UIImage systemImageNamed:@"person.circle"];
    [_avatar sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:ph];
    _nameL.text = name ?: @"用户";
    _introL.text = intro ?: @"这个人很懒，什么都没有写。";
}
@end
